<template>
  <div>
    <div class="flex justify-center py-4">
      <img src="~@/assets/logo.png" class="h-6" alt="logo" />
      <span class="ml-4 text-white" v-if="!collapse">Vue3+Ts</span>
    </div>

    <el-menu
      class="el-menu-vertical"
      :default-active="defaultActive"
      background-color="#0c2135"
      text-color="#b7bdc3"
      active-text-color="#0a60bd"
      :collapse="collapse"
    >
      <template v-for="item in userMenus" :key="item.id">
        <!--submenu-->
        <template v-if="item.type === 1">
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <i v-if="item.icon" :class="item.icon"></i>
              <span>{{ item.name }}</span>
            </template>
            <template v-for="subitem in item.children" :key="subitem.id">
              <el-menu-item
                :index="subitem.id + ''"
                @click="handleMenuItemClick(subitem)"
              >
                <i v-if="subitem.icon" :class="subitem.icon"></i>
                <span>{{ subitem.name }}</span>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>

        <!--submenuitem-->
        <template v-else-if="item.type === 2">
          <el-menu-item :index="item.id + ''">
            <i v-if="item.icon" :class="item.icon"></i>
            <span>{{ item.name }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script setup lang="ts">
import { computed, defineProps, ref } from 'vue'
import { useStore } from '@/store'
import { useRoute, useRouter } from 'vue-router'
import { pathMapToMenu } from '@/utils/map-menus'

const props = defineProps({
  collapse: {
    type: Boolean,
    default: false
  }
})
const route = useRoute()
const router = useRouter()
const store = useStore()
const userMenus = computed(() => store.state.login.userMenus)
const handleMenuItemClick = (item: any) => {
  router.push({
    path: item.url ?? '/not-found'
  })
}
const currentPath = route.path
const menu = pathMapToMenu(userMenus.value, currentPath)
const defaultActive = ref(menu.id + '')
</script>

<style scoped>
.el-menu {
  border-right: none;
}
</style>
